package com.example.sequences.service;

import com.example.sequences.dao.StudentDao;
import com.example.sequences.domain.Student;
import com.example.sequences.dto.StudentDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * PersonService
 *
 * @author maxsc
 * @title PersonService.java
 * @date 2018/5/8
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StudentService {
    @Autowired
    private StudentDao studentDao;

    public List<StudentDto> queryAll(){
        List<Student> all = studentDao.findAll ();
        List<StudentDto> list = new ArrayList<> (  );
        all.stream ().forEach ( student -> {
            StudentDto studentDto = new StudentDto ();
            studentDto.setId ( student.getId () );
            studentDto.setAge ( student.getAge () );
            studentDto.setGender ( student.getGender () );
            studentDto.setName ( student.getName () );
            studentDto.setGrade ( student.getGrade () );
            studentDto.setScore ( student.getScore () );
            studentDto.setMobile ( student.getMobile () );
            list.add ( studentDto );
        } );
        return list;
    }
    public StudentDto findOne(String id){
        Student student = studentDao.findOne ( id );
        StudentDto studentDto = new StudentDto ();
        studentDto.setId ( student.getId () );
        studentDto.setAge ( student.getAge () );
        studentDto.setGender ( student.getGender () );
        studentDto.setName ( student.getName () );
        studentDto.setGrade ( student.getGrade () );
        studentDto.setScore ( student.getScore () );
        studentDto.setMobile ( student.getMobile () );
        return studentDto;
    }
    public void ins(Student student){
        studentDao.save ( student );
    }
    public void up(StudentDto studentDto){
        Student one = studentDao.findOne ( studentDto.getId () );
        one.setGrade ( studentDto.getGrade () );
        one.setScore ( studentDto.getScore () );
        one.setAge ( studentDto.getAge () );
        one.setGender (studentDto.getGender ());
        one.setName ( studentDto.getName () );
        one.setMobile ( studentDto.getMobile () );
    }
    public void up2(String grade,String score,String name){
        studentDao.updateStu ( grade,score,name );
    }
}
